﻿package com.tvein.sanguo.swc.crowd
{
	//flash
	import fl.events.ScrollEvent;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.geom.Transform;
	import flash.geom.Matrix;
	//A3D
	//custom
	import com.tvein.sanguo.swc.crowd.Human;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class CrowdShell extends MovieClip
	{
		public static const NAME:String = "CrowdShell";
		
		private var gameConfig:Array;
		
		public function CrowdShell(configArray:Array)
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
			
			gameConfig = configArray;
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			mcFrame.cacheAsBitmap = true;
			uiScroll.addEventListener(ScrollEvent.SCROLL , _onScroll);
			
			_resizeUI();
		}
		
		private function _onScroll(evt:ScrollEvent):void
		{
			var rect:Rectangle = mcFrame.scrollRect;
			rect.y = uiScroll.scrollPosition;
			mcFrame.scrollRect = rect;
		}
		
		public function roleClear():void
		{
			mcFrame.Clear();
			_resizeUI();
		}
		
		public function roleAdd(roleID:String, roleName:String, roleFace:int, roleCMD:Object):void
		{
			mcFrame.Add(roleID, roleName, roleFace, roleCMD, gameConfig);
			_resizeUI();
		}
		
		public function roleDelete(roleID:String):void
		{
			mcFrame.Delete(roleID);
			_resizeUI();
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
		}
		
		private function _resizeUI(evt:Event = null):void
		{
			var headNum:int = Math.floor((stage.stageHeight - 110) / 125);
			
			this.x = stage.stageWidth - 100;
			this.y = (stage.stageHeight - 125 * headNum) * .5;
			
			var frameHeight:Number = getFullBounds(mcFrame).height;
			var rectHeight:Number = 125 * headNum;
			
			var rect:Rectangle = new Rectangle(0, 0, 80, rectHeight);
			mcFrame.scrollRect = rect;
			
			uiScroll.setScrollProperties(rectHeight, 0, frameHeight - rectHeight);
			uiScroll.setSize(15, rectHeight);
			uiScroll.update();

			if (frameHeight > rectHeight)
			{
				uiScroll.visible = true;
			}
			else
			{
				uiScroll.visible = false;
			}
		}
		
		private function getFullBounds(displayObject:MovieClip):Rectangle
		{
			var bounds:Rectangle, transform:Transform, toGlobalMatrix:Matrix, currentMatrix:Matrix;
			
			transform = displayObject.transform;
			currentMatrix = transform.matrix;
			toGlobalMatrix = transform.concatenatedMatrix;
			toGlobalMatrix.invert();
			transform.matrix = toGlobalMatrix;
			bounds = transform.pixelBounds.clone();
			transform.matrix = currentMatrix;
			
			return bounds;
		}
	}
}